from django.db import models
from user.models import User


class BaseModel(models.Model):
    """模型抽象基类"""
    is_delete = models.BooleanField(default=False, verbose_name='删除标记')

    class Meta:
        # 说明是一个抽象模型类
        abstract = True


class ReasonType(BaseModel):
    name = models.CharField(max_length=100)
    is_stu_ask = models.BooleanField(default=True)

    class Meta:
        db_table = 'reason_type'


class Leave(BaseModel):
    CHOICES = (
        ('审核中', 1),
        ('未通过', 2),
        ('已通过', 3),
    )
    ask_user = models.ForeignKey(User, related_name='leaves', on_delete=models.CASCADE)
    reason_type = models.ForeignKey(ReasonType, related_name='leaves', on_delete=models.CASCADE)
    description = models.TextField()
    ask_time = models.DateTimeField(auto_now_add=True)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    status = models.CharField(choices=CHOICES, default='审核中', max_length=10)
    is_lengthen = models.BooleanField(default=False)
    is_back = models.BooleanField(default=False)
    examine_user = models.ManyToManyField(User, through='ExamineLeave')

    class Meta:
        db_table = 'leave'


class ExamineLeave(models.Model):
    leave = models.ForeignKey(Leave, related_name='examine', on_delete=models.CASCADE)
    user = models.ForeignKey(User, related_name='examine', on_delete=models.CASCADE)
    is_agree = models.BooleanField(default=False)

    class Meta:
        db_table = 'examine_leave'
